﻿using apitest.Application.Entities.Pms;
using apitest.Application.Test.Models;
using MiniExcelLibs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Reflection;

namespace apitest.Application.Test.Services
{
    public class Test1Service 
    {
        ISqlSugarClient _db;
        public Test1Service(ISqlSugarClient db)
        {
            _db = db;   
        }

        [HttpGet]
        public async Task<dynamic> T1()
        {
            DateTime start = DateTime.Parse("2024-10-1 09:09:09");
            DateTime end = DateTime.Parse("2024-10-03 16:09:15");
            TimeSpan daysSpan = new TimeSpan(end.Ticks - start.Ticks);
            var holidays = new List<DateTime>() {
                DateTime.Parse("2024-10-01") ,
                DateTime.Parse("2024-10-02") ,
                DateTime.Parse("2024-10-03") ,
                DateTime.Parse("2024-10-04") ,
                DateTime.Parse("2024-10-05") ,
                DateTime.Parse("2024-10-06") ,
                DateTime.Parse("2024-10-07") ,
            };
            return ComputedMinutes(start, end, holidays);
        }

        public int ComputedMinutes(DateTime start, DateTime end, List<DateTime> holidays)
        {
            var startDate = start.Date;
            var endDate = end.Date;
            var diffDays = (endDate - startDate).Days;
            var res = DateHelper.GetDateDiff(start, end, 2);
            if (diffDays < 2)
            {
                return res;
            }
            else
            {
                var total = 0;
                while (startDate != endDate.AddDays(-1))
                {
                    startDate = startDate.AddDays(1);
                    if (holidays.Contains(startDate))
                    {
                        total += 1;
                    }
                }
                return res - total * 1440;
            }
        }

        [HttpGet, NonUnify]
        public async Task<dynamic> T2()
        {
            var str = """
[{"name":"HPLC","price":0,"hour":0,"group":"","_XID":"row_37"},{"name":"GC","price":0,"hour":0,"group":"","_XID":"row_36"},{"name":"KF","price":0,"hour":0,"group":"","_XID":"row_35"}][{"name":"HPLC","price":0,"hour":0,"group":"","_XID":"row_37"},{"name":"GC","price":0,"hour":0,"group":"","_XID":"row_36"},{"name":"KF","price":0,"hour":0,"group":"","_XID":"row_35"}]
""";
            var list = new List<test_user>();
            list.Add(new test_user()
            {
                id = 1,
                name = "aa"
            });
            list.Add(new test_user()
            {
                id = 2,
                name = "bb"
            });
            var user = new test_user()
            {
                id = 2,
                name = "bb"
            };
            str = JsonConvert.SerializeObject(user);

            Assembly ass = Assembly.Load("apitest.Application");
            var aa = ass.GetType("apitest.Application.Test.Models.test_user");

            return JsonConvert.DeserializeObject(str, aa);


            var jarray = JArray.Parse(str);

            var list1 = new List<dynamic>();
            foreach (var ja in jarray)
            {
                list1.Add(JsonConvert.DeserializeObject(ja.ToString()) as JObject);
            }
            return list1;
        }

        public async Task<dynamic> T3()
        {
            var list = await MiniExcel.QueryAsync("./123123.xlsx",useHeaderRow:true,sheetName:"岳池工时");
            return list;
        }
 
    }

}
